package com.ehotting.edsta.circuitDesign.controller;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ehotting.eaf.core.domain.R;
import com.ehotting.edsta.circuitDesign.domain.EsCircuit;
import com.ehotting.eaf.core.utils.poi.ExcelUtil;
import com.ehotting.eaf.core.web.controller.BaseController;
import com.ehotting.eaf.core.web.domain.AjaxResult;
import com.ehotting.eaf.core.web.page.TableDataInfo;
import com.ehotting.edsta.common.log.enums.BusinessType;
import com.ehotting.edsta.common.log.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.ehotting.edsta.circuitDesign.service.IEsCircuitService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 应用场景Controller
 * 
 * @author edsta
 * @date 2023-05-05
 */
@RestController
@RequestMapping("/circuit") // /circuitDesign要去调，现在测试用，在本地测试
public class EsCircuitController extends BaseController
{
    @Value(value = "${circuitFilesIp}")
    private String circuitFilesIp;
    @Autowired
    private IEsCircuitService esCircuitService;


    /**
     * 查询应用场景列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EsCircuit esCircuit)
    {
        startPage();
        List<EsCircuit> list = esCircuitService.selectEsCircuitList(esCircuit);
        return getDataTable(list);
    }

    @GetMapping("/allList")
    public AjaxResult allList()
    {

        List<EsCircuit> list = esCircuitService.selectEsCircuitList(null);
        return AjaxResult.success(list);
    }

    /**
     * 导出应用场景列表
     */
    @Log(title = "应用场景", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EsCircuit esCircuit) throws IOException
    {
        List<EsCircuit> list = esCircuitService.selectEsCircuitList(esCircuit);
        ExcelUtil<EsCircuit> util = new ExcelUtil<EsCircuit>(EsCircuit.class);
        util.exportExcel(response, list, "应用场景数据");
    }

    /**
     * 获取应用场景详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,HttpServletRequest request)
    {
        String authorization = request.getHeader("Authorization");
        String newToken = "&token="+authorization.replace("Bearer ","");
        EsCircuit esCircuit = esCircuitService.selectEsCircuitById(id);
        esCircuit.setThumbnailPath(circuitFilesIp + esCircuit.getThumbnailPath() + newToken);
        esCircuit.setThumbPath(circuitFilesIp + esCircuit.getThumbPath() + newToken);
        esCircuit.setChartsPath(circuitFilesIp + esCircuit.getChartsPath() + newToken);
        return AjaxResult.success(esCircuit);
    }

    /**
     * 新增应用场景
     */
    @Log(title = "应用场景", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EsCircuit esCircuit)
    {

        return toAjax(esCircuitService.insertEsCircuit(esCircuit));
    }

    /**
     * 修改应用场景
     */
    @Log(title = "应用场景", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EsCircuit esCircuit)
    {
        return toAjax(esCircuitService.updateEsCircuit(esCircuit));
    }

    /**
     * 删除应用场景
     */
    @Log(title = "应用场景", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(esCircuitService.deleteEsCircuitByIds(ids));
    }

    /**
     * 查询应用场景列表
     */
    @GetMapping("/execute")
    public AjaxResult execute(@RequestParam Long circuitId, @RequestParam Long ruleId, @RequestParam Map<String,Object>  params)
    {
        return AjaxResult.success(esCircuitService.execute(circuitId, ruleId, params));
    }

    /**
     * 批量导入电路
     */
    @PostMapping("/importExcel")
    public R importExcel(@RequestParam MultipartFile file,String step) {
        logger.info("fileName:" + file.getOriginalFilename());
        return esCircuitService.circuitExcelDataCheck(file,step);
    }

    /**
     * 加载电路属性进缓存
     */
    @GetMapping("/loadProperty")
    public AjaxResult loadProperty() {
        esCircuitService.loadProperty();
        return AjaxResult.success("属性加载成功");
    }

    /**
     * 上传目录结构进缓存
     */
    @PostMapping("/filesVerify")
    public AjaxResult filesVerify(@RequestBody String fileNames){
        JSONObject jsonObject = JSONObject.parseObject(fileNames);
        JSONArray fileNameArray = jsonObject.getJSONArray("fileNames");
        esCircuitService.filesVerify(fileNameArray);
        return AjaxResult.success("上传成功");
    }

    /**
     * 获取电路数据表格的缺失文件
     */
    @GetMapping("/fileLackList")
    public AjaxResult fileLackList(@RequestParam int page){
        return AjaxResult.success(esCircuitService.getFileLackList(page));
    }

    @GetMapping("/fileLackListDownload")
    public AjaxResult fileLackListDownload(){
        return AjaxResult.success(esCircuitService.fileLackListDownload());
    }

    @GetMapping("/noExistList")
    public AjaxResult noExistList(@RequestParam int page){
        return AjaxResult.success(esCircuitService.getNoExistList(page));
    }

    @GetMapping("/fileNoExistDownload")
    public AjaxResult fileNoExistDownload(){
        return AjaxResult.success(esCircuitService.NoExistListDownload());
    }

    @GetMapping("/checkDocumentLibrary")
    public AjaxResult checkDocumentLibrary(){
        return AjaxResult.success(esCircuitService.checkDocumentLibrary());
    }

    @GetMapping("/checkDocumentLibraryDownload")
    public AjaxResult checkDocumentLibraryDownload(){
        return AjaxResult.success(esCircuitService.checkDocumentLibraryDownload());
    }
}
